﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Entidad;
using System.Data.SqlClient;
using System.Data;

namespace Datos
{
    public class DaProveedores
    {
        public int updateProveedor(string proveedor, string password)
        {
            string cadCon = ConfigurationManager.AppSettings["connString"].ToString();
            int res = 0;
            using (SqlConnection cn = new SqlConnection(cadCon))
            {
                try
                {
                    cn.Open();
                    using (SqlCommand cmd = new SqlCommand("proc_proveedor_up", cn))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add("@proveedor", SqlDbType.NVarChar).Value = proveedor;
                        cmd.Parameters.Add("@password", SqlDbType.NVarChar).Value = password;
                        res = Convert.ToInt16(cmd.ExecuteScalar());
                    }
                }
                catch
                {
                    res = 0;
                }
                
            }

            return res;
        }

        public EnProveedor validaLogin(string proveedor, string password)
        {
            string cadCon = ConfigurationManager.AppSettings["connString"].ToString();
            SqlDataReader dr1;
            EnProveedor objR = null;
            using (SqlConnection cn = new SqlConnection(cadCon))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("proc_login_se", cn))
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.Add("@proveedor", SqlDbType.NVarChar).Value = proveedor;
                    cmd.Parameters.Add("@password", SqlDbType.NVarChar).Value = password;

                    dr1 = cmd.ExecuteReader();
                    while (dr1.Read())
                    {
                        objR = new EnProveedor();
                        objR.documento_proveedor = dr1[0].ToString();
                        objR.lifnr = dr1[1].ToString();
                        objR.descripcion = dr1[2].ToString();
                    }
                    dr1.Close();
                }
            }

            return objR;
        }


        public List<EnCombo> getSociedadesXProvee(String provee)
        {
            List<EnCombo> lst = new List<EnCombo>();
            try
            {
                string cadCon = ConfigurationManager.AppSettings["connString"].ToString();
                SqlDataReader dr1;
                using (SqlConnection cn = new SqlConnection(cadCon))
                {
                    cn.Open();
                    using (SqlCommand cmd = new SqlCommand("proc_sociedad_extra_se", cn))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add("@proveedor", SqlDbType.NVarChar).Value = provee;
                        EnCombo item = new EnCombo();
                        item.codigo = "";
                        item.decrip = "";
                        lst.Add(item);
                    
                        dr1 = cmd.ExecuteReader();
                        while (dr1.Read())
                        {
                            EnCombo it = new EnCombo();
                            it.codigo = dr1[0].ToString();
                            it.decrip = dr1[1].ToString();
                            lst.Add(it);
                        }
                        dr1.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                lst = new List<EnCombo>();
            }

            return lst;
        }
    }
}
